#!/bin/sh

#get user input
cdrecord -scanbus
echo ""
echo "I found these devices on your computer."
echo "Each device follows the general form 9,#,# or 8,#,#"
read -p "Enter the default device to WRITE to (i.e 9,0,0) : " writedevpos
read -p "Please enter the desired write speed. (i.e 1 2 4 6 8...) : " devicespeed

			# Detect CD-ROM
			TEMPDEVICE=$RawPath
			echo
			echo "Auto-detecting connected CD-ROM devices..."
			echo
			
			# Use the BeOS play command to get what we need.
			# grep its output for the device paths.
			# Last sed of line below makes it possible to handle enhanced CDs properly (thanks Jon Watte).
			
			play | grep "/dev" | sed s@\ @@g | sed -e s@raw\$@@ > /tmp/cddev.txt
			
			{
				# Create list of CD-ROM drives to choose from
				i=1
				while read ThisDevice; do
					echo $i\) $ThisDevice
					i=$(expr $i + 1)
				done
			} < /tmp/cddev.txt > /tmp/cdlist.txt
			
			# Report back list of numbered devices
			cat /tmp/cdlist.txt
						
			echo
			echo "Your CD-ROM drive(s) are listed above."
			echo "Choose one from the list (you might have only one): "
			
			# User picks a device by number
			read DevNum
			
			# Make sure they didn't just enter a space or hit Return.
			if [ -z "$DevNum" ]; then
				echo "That is not a valid response. Please re-run CD-ROM "
				echo "device detection and choose a number from the list."
				echo "Hit Enter to continue."
				read continue
				BuildMenu
			fi
			
			# Check to see the number they entered is on the list.
			# This also checks to see that they didn't enter a letter.
			if
				grep ^$DevNum /tmp/cdlist.txt
				then
				echo
		
			else
			echo "That is not a valid response. Please re-run CD-ROM "
				echo "device detection and choose a number from the list."
				echo "Hit Enter to continue."
				read continue
				BuildMenu			
			fi     
			
			
			# Find this number in cdlist.txt and remove the number,
			# parenthesis, and space from the variable. We'll now have
			# a usable device path.
			RawPath=$(grep ^$DevNum /tmp/cdlist.txt | sed s/^$DevNum\)\ //)

			# In case of error...
			if [ ! -e $RawPath ] ;
			then
				echo $RawPath "does not exist...hit enter to continue"
				read continue
				RawPath=$TEMPDEVICE
			fi
			if [ "$RawPath" = "" ] ;
			then
  				RawPath=$TEMPDEVICE
			fi
echo ''
read -p "Please enter the default BFS image size: " bfsimagesize
read -p "Please enter the default creation point for images (i.e. /boot/home) : " imagepoint

# Link terminal to /boot/home/config/bin/Terminal
ln -s /boot/beos/apps/Terminal /boot/home/config/bin/Terminal 2> /dev/null


#Trash the old settings

rm -f /boot/home/config/settings/writecd_prefs/writedevpos
rm -f /boot/home/config/settings/writecd_prefs/readdevpos
rm -f /boot/home/config/settings/writecd_prefs/devicespeed
rm -f /boot/home/config/settings/writecd_prefs/bfsimagesize
rm -f /boot/home/config/settings/writecd_prefs/imagepoint

#wite the new ones
mkdir /boot/home/config/settings/writecd_prefs 2> /dev/null

echo $writedevpos > /boot/home/config/settings/writecd_prefs/writedevpos
echo $RawPath > /boot/home/config/settings/writecd_prefs/readdevpos
echo $devicespeed > /boot/home/config/settings/writecd_prefs/devicespeed
echo $bfsimagesize > /boot/home/config/settings/writecd_prefs/bfsimagesize
echo $imagepoint> /boot/home/config/settings/writecd_prefs/imagepoint

echo ""
read -p "Please press enter to quit. All options configured, you may now run MakeCD"

